package com.hz.ws;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.hz.base.beans.vo.CommonResult;
import com.hz.base.beans.vo.PageEntity;
import com.hz.base.beans.vo.QueryParams;
import com.hz.beans.user.entity.BaseRole;
import com.hz.interfaces.user.inf.IBaseRoleInterface;
import com.hz.sys.utils.RedisUtil;
import com.hz.utils.ImportExcelUtil;
import com.hz.utils.ExportExcelUtil;

import io.swagger.annotations.*;

@Api(tags = "用户管理", description = "operation about user")
@RestController
@RequestMapping("/user")
public class TestController {
    @Autowired
    private IBaseRoleInterface baseRoleInterface;
    @Autowired
    private RedisUtil redis;

    //	@PreAuthorize("hasAuthority('ROLE_WW')")
    @ApiOperation(value = "获取角色信息", notes = "根据url的id来获取角色详细信息")
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long", paramType = "query")
    @RequestMapping(value = "/query", method = {RequestMethod.POST, RequestMethod.GET})
    public CommonResult<BaseRole> testQuery(@RequestParam Long id) {
        redis.set("id", id);
        System.out.println(redis.get("id"));
        return baseRoleInterface.queryRole(id);
    }

    @ApiOperation(value = "获取角色page", notes = "根据url的queryParams来获取角色列表详细信息")
    @RequestMapping(value = "/queryForPage", method = {RequestMethod.POST, RequestMethod.GET})
    public CommonResult<PageEntity> queryForPage(@RequestBody QueryParams<BaseRole> queryParams) {
        return baseRoleInterface.queryForPage(queryParams);
    }

    @ApiOperation(value = "添加角色", notes = "根据post参数信息添加角色")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<BaseRole> add(Long[] menuIds, @RequestBody BaseRole baseRole) {
        return baseRoleInterface.addRole(baseRole, menuIds);
    }

    @ApiOperation(value = "下载", notes = "下载excel")
    @RequestMapping(value = "/exportData", method = RequestMethod.GET)
    public boolean exportData(BaseRole baseRole, QueryParams<BaseRole> queryParams, HttpServletResponse response) {
        queryParams.setEntity(baseRole);
        CommonResult<List<BaseRole>> cr = baseRoleInterface.queryRoleList(queryParams);
        List<BaseRole> baseRoleList = cr.getData();
        // 定义导出excel的名字
        String excelName = "角色表";
        // 获取需要转出的excle表头的map字段
        LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
        fieldMap.put("id", "id");
        fieldMap.put("parentId", "父角色id");
        fieldMap.put("name", "角色名称");
        fieldMap.put("remarks", "备注信息");
        fieldMap.put("createBy", "创建者");
        fieldMap.put("createTime", "创建时间");
        fieldMap.put("updateBy", "更新人");
        fieldMap.put("updateTime", "更新时间");

        // 导出所有会员信息表
        return ExportExcelUtil.export(excelName, baseRoleList, fieldMap, response);
    }

    @ApiOperation(value = "导入excel数据", notes = "根据excel内容导入数据")
    @RequestMapping(value = "/importData", method = RequestMethod.POST)
    public String importData(MultipartFile file) throws Exception {
        Map<String, String> fieldMap = new HashMap<String, String>();
        fieldMap.put("父角色id", "parentId");
        fieldMap.put("角色名称", "name");
        fieldMap.put("备注信息", "remarks");
        ImportExcelUtil export = new ImportExcelUtil(fieldMap);
        export.init(file.getInputStream());
        List<BaseRole> baseRoleList = export.bindToModels(BaseRole.class, false);
        for (BaseRole baseRole : baseRoleList) {
            baseRoleInterface.addRole(baseRole, null);
        }
        if (export.hasError()) {
            System.out.println(export.getError().toString());
            return export.getError().toString();
        }
        return "success";
    }

}
